package cn.lxh.ymcc.service.impl;

import cn.lxh.ymcc.domain.KillActivity;
import cn.lxh.ymcc.domain.KillCourse;
import cn.lxh.ymcc.mapper.KillCourseMapper;
import cn.lxh.ymcc.service.IKillActivityService;
import cn.lxh.ymcc.service.IKillCourseService;
import cn.lxh.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 在秒杀course中直接
 *          -增加秒杀
 *
 * @author small.tiger
 * @since 2022-08-04
 */
@Service
public class KillCourseServiceImpl extends ServiceImpl<KillCourseMapper, KillCourse> implements IKillCourseService {
    @Autowired
    private IKillActivityService killActivityService;

    @Override
    public void save(KillCourse killCourse) {
        KillCourse killCourseTmp = selectByIdAndCourseID(killCourse.getActivityId(), killCourse.getCourseId());
        AssertUtil.isNull(killCourseTmp,"秒杀课程已经存在！");

        //同一个课程不能加入到同一个活动下面
        KillActivity killActivity = killActivityService.selectById(killCourse.getActivityId());
        killCourse.setStartTime(killActivity.getStartTime());
        killCourse.setEndTime(killActivity.getEndTime());
        killCourse.setTimeStr(killActivity.getTimeStr());
        killCourse.setCreateTime(new Date());
        insert(killCourse);
    }

    private KillCourse selectByIdAndCourseID(Long activityId, Long courseId) {
        Wrapper<KillCourse> wrapper = new EntityWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("activity_id",activityId);
        return selectOne(wrapper);
    }

}
